Home:ALL Converter>What is the difference between different ways of attaching\detaching event handlers in C#

What is the difference between different ways of attaching\detaching event handlers in C#

Ask Time:2010-12-06T02:33:43         Author:Unmesh Kondolikar

Json Formatter

There are two parts to my question -

Firstly we can attach event handlers in following two ways -

myObject.MyEvent += new EventHandler(MyHandler);

myObject.MyEvent += MyHandler;

As per my understanding these two are equivalent. In the second case the C# compiler does the job of creating a delegate instance from the appropriate overload from the specified method group. Is this correct?

Secondly, is there any difference between the two corresponding styles of detaching the handler? If yes then what is it?

 myObject.MyEvent -= new EventHandler(MyHandler);

 myObject.MyEvent -= MyHandler;

Author:Unmesh Kondolikar,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/4360451/what-is-the-difference-between-different-ways-of-attaching-detaching-event-handl
Marc Gravell :

They are identical, unless you are in c# 1.2 where only the first compiles.",
2010-12-05T18:42:57
yy